/*
 * Created on 2008-06-04
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package elka.isi.operator;

import org.evolvica.core.IIndividual;
import org.evolvica.core.IIndividualSet;
import org.evolvica.core.IndividualArray;

import elka.isi.util.*;

/**
 * @author Zibi
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class HuntingEcosystem extends SortedEcosystem {

	/* (non-Javadoc)
	 * @see elka.isi.SortedEcosystem#insert(org.evolvica.core.IIndividualSet)
	 */
	public IIndividualSet insert(IIndividualSet iset) {
		RWUtils.getActualStats(iset);
		{
			if (size() != 0) {
				IIndividual[] wolves = wolvesPopulation.toArray();
				IIndividual[] rabbits = rabbitsPopulation.toArray();
				for (int i = 0; i < wolves.length; i++) {
					IIndividualSet weakerRabbits =
						RWUtils.getWeaker(rabbits, wolves[i], comparator);
					IIndividualSet rabbitsToKill =
						RWUtils.getRandomIndividuals(
							weakerRabbits,
							wolfApetite);
					if (rabbitsToKill.size() < wolfApetite)
						wolvesPopulation.remove(wolves[i]);
					rabbitsPopulation.remove(rabbitsToKill.toArray());
				}
			}

			wolvesPopulation.getFilter().insert(this, RWUtils.getWolves(iset));
			rabbitsPopulation.getFilter().insert(
				this,
				RWUtils.getRabbits(iset));

			++generation; // increment generation counter
			IIndividualSet current =
				new IndividualArray(
					wolvesPopulation.size() + rabbitsPopulation.size());
			current.add(wolvesPopulation.toArray());
			current.add(rabbitsPopulation.toArray());

			return current;

		}

	}
}